package com.liting.uitest.imgutils;

import com.liting.uitest.model.Circle;
import com.liting.uitest.model.ImageInfo;
import com.liting.uitest.model.Rectangle;
import com.liting.uitest.service.impl.ImageCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * 简单感SI计算
 */
@Service
public class SimplicityCalc {
    @Autowired
    private ImageCacheService cacheService;

    public BigDecimal siCalc(Long fileId, List<Rectangle> rectList, List<Circle> cirList) throws Exception {
        ImageInfo imageInfo = cacheService.getImage(fileId);
        if (imageInfo == null) {
            throw new Exception("文件不存在");
        }

        int nvCount = 0, nhCount = 0;
        List<Double> xList = new ArrayList<>();
        List<Double> yList = new ArrayList<>();

        List<Double> containX = new ArrayList<>();
        List<Double> containY = new ArrayList<>();

        for (Rectangle rectangle : rectList) {
            double tempX = rectangle.getLeftTopX();
            if (xList.contains(tempX)) {
                if (containX.contains(tempX)) {
                    nvCount++;
                } else {
                    nvCount = nvCount + 2;
                    containX.add(tempX);
                }
            } else {
                xList.add(tempX);
            }

            double tempY = rectangle.getLeftTopY();
            if (yList.contains(tempY)) {
                if (containY.contains(tempY)) {
                    nhCount++;
                } else {
                    nhCount = nhCount + 2;
                    containY.add(tempY);
                }
            } else {
                yList.add(tempY);
            }
        }

        return BigDecimal.valueOf((nvCount + nhCount) / (double) (4 * rectList.size())).setScale(1, RoundingMode.UP);
    }
}
